package com.xzp.bams.entity;

import com.xzp.bams.exception.LoanException;

import java.io.Serializable;

/**
 * @TODO
 * @Author xu
 * 2025/3/10
 */
public class LoanCreditAccount extends CreditAccount implements Loanable, Serializable {
  private Long loanAccount;
    private double loanAmount;



    /**
     * 贷款
     * @param money
     * @return
     */
    @Override
 public  Account requestLoan(double money)throws LoanException
 {
     if(money>0){
         this.loanAmount+=money;
         this.setBalance(this.getBalance() + money);
     }else{
         //System.out.println("贷款额度>0");
         throw new LoanException("贷款额度>0");
     }
     return this;
 }
/**
 * 还款
 * @param money
 * @return
 */
  @Override
    public Account payLoan(double money)throws LoanException
    {
        if(money>0){
            if(money<=loanAmount){
                if(this.getBalance() + this.getCeiling() >= money){
                    if(this.getBalance()>=money){
                        this.loanAmount-=money;
                        this.setBalance(this.getBalance() - money);
                    }else{
                        this.loanAmount-=money;
                        this.setCeiling(this.getCeiling() - (money-this.getBalance()));  //1500-(6000-5000)=500.0
                        this.setBalance(0.0);  //0.0
                    }
                }else{
                    //System.out.println("您的账户余额不够支付还款操作！");
                    throw new LoanException("您的账户余额不够支付还款操作！");
                }
            }else{
                //System.out.println("还款金额<=贷款金额，");
                throw new LoanException("还款金额<=贷款金额，");
            }
        }else{
            //System.out.println("还款额度>0");
            throw new LoanException("还款额度>0");
        }
        return this;
    }

    public LoanCreditAccount() {

    }

    public LoanCreditAccount(Long id, String password, String name, String personId, String email, double balance, double ceiling, Long loanAmount) {
        super(id, password, name, personId, email, balance, ceiling);
        this.loanAmount = loanAmount;
    }


    /**
     * 获取
     * @return loanAccount
     */
    public Long getLoanAccount() {
        return loanAccount;
    }

    /**
     * 设置
     * @param loanAccount
     */
    public void setLoanAccount(Long loanAccount) {
        this.loanAccount = loanAccount;
    }

    @Override
    public String toString() {
        return "LoanCreditAccount{" +
                "loanAccount=" + loanAccount +
                "}"+ super.toString();
    }

    /**
     * 获取
     * @return loanAmount
     */
    public double getLoanAmount() {
        return loanAmount;
    }

    /**
     * 设置
     * @param loanAmount
     */
    public void setLoanAmount(double loanAmount) {
        this.loanAmount = loanAmount;
    }
}
